// pages/substitute-class/detail/detail.js
Page({
  data: {
    courseInfo: {},
    loading: true
  },

  onLoad(options) {
    const { id } = options
    if (id) {
      this.loadCourseDetail(id)
    }
  },

  loadCourseDetail(id) {
    wx.showLoading({ title: '加载中...' })
    
    // 模拟API调用
    setTimeout(() => {
      const mockData = {
        id: id,
        courseName: '高等数学',
        teacher: '张教授',
        location: '教学楼A101',
        time: '2024-01-20 14:00-16:00',
        reward: 50,
        urgent: true,
        description: '因有急事无法上课，寻找代课同学。需要认真听讲并做好笔记，课后将笔记拍照发给我。',
        requirements: '需要有相关课程基础，能够认真听讲做笔记',
        publishTime: '2024-01-18 10:30',
        publisherName: '李同学',
        publisherAvatar: '/images/avatar-default.png',
        publisherCredit: 95,
        publisherCollege: '计算机学院',
        status: 'pending', // pending, accepted, completed
        isOwner: false,
        accepter: null,
        accepterName: '',
        accepterAvatar: '',
        accepterCredit: 0,
        acceptTime: ''
      }
      
      this.setData({
        courseInfo: mockData,
        loading: false
      })
      wx.hideLoading()
    }, 1000)
  },

  acceptOrder() {
    wx.showModal({
      title: '确认接单',
      content: '确定要接受这个代课任务吗？',
      success: (res) => {
        if (res.confirm) {
          wx.showLoading({ title: '处理中...' })
          
          // 模拟API调用
          setTimeout(() => {
            wx.hideLoading()
            wx.showToast({
              title: '接单成功',
              icon: 'success'
            })
            
            // 更新状态
            this.setData({
              'courseInfo.status': 'accepted',
              'courseInfo.accepter': 'current_user',
              'courseInfo.accepterName': '当前用户',
              'courseInfo.accepterAvatar': '/images/avatar-default.png',
              'courseInfo.accepterCredit': 98,
              'courseInfo.acceptTime': '2024-01-18 15:30'
            })
          }, 1500)
        }
      }
    })
  },

  cancelOrder() {
    wx.showModal({
      title: '确认取消',
      content: '确定要取消这个代课发布吗？',
      success: (res) => {
        if (res.confirm) {
          wx.showLoading({ title: '处理中...' })
          
          setTimeout(() => {
            wx.hideLoading()
            wx.showToast({
              title: '已取消发布',
              icon: 'success'
            })
            
            setTimeout(() => {
              wx.navigateBack()
            }, 1500)
          }, 1000)
        }
      }
    })
  },

  confirmComplete() {
    wx.showModal({
      title: '确认完成',
      content: '确认代课任务已完成？',
      success: (res) => {
        if (res.confirm) {
          wx.showLoading({ title: '处理中...' })
          
          setTimeout(() => {
            wx.hideLoading()
            wx.showToast({
              title: '已确认完成',
              icon: 'success'
            })
            
            this.setData({
              'courseInfo.status': 'completed'
            })
          }, 1000)
        }
      }
    })
  },


  contactPublisher() {
    wx.navigateTo({
      url: `/pages/chat/chat?userId=${this.data.courseInfo.publisherId}&userName=${this.data.courseInfo.publisherName}`
    })
  }
})